package com.yearEndWorkload.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yearEndWorkload.common.Result;
import com.yearEndWorkload.pojo.entity.TypeStandard;
import com.yearEndWorkload.pojo.form.add.AddTypeStandardForm;
import com.yearEndWorkload.pojo.form.add.CopyTypeStandardForm;
import com.yearEndWorkload.pojo.query.TypeStandardQuery;
import com.yearEndWorkload.pojo.vo.BaseVo;
import com.yearEndWorkload.service.TypeStandardService;
import com.yearEndWorkload.mapper.TypeStandardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author admin
* @description 针对表【type_standard(工作量类型标准表)】的数据库操作Service实现
* @createDate 2024-03-06 22:44:45
*/
@Service
public class TypeStandardServiceImpl extends ServiceImpl<TypeStandardMapper, TypeStandard>
    implements TypeStandardService{

    @Autowired
    TypeStandardMapper typeStandardMapper;

    @Override
    public Result getList(TypeStandardQuery typeStandardQuery) {
        Page<TypeStandard> page = new Page<>(typeStandardQuery.getCurrentPage(),typeStandardQuery.getPageSize());
        Page<TypeStandard> re =typeStandardMapper.getList(page,typeStandardQuery);

        BaseVo<TypeStandard> typeStandardBaseVo = new BaseVo<>();

        typeStandardBaseVo.setCurrentPage(re.getCurrent());
        typeStandardBaseVo.setRecords(re.getRecords());
        typeStandardBaseVo.setAllPages(re.getPages());
        typeStandardBaseVo.setAllRecordNum(re.getTotal());
        typeStandardBaseVo.setPageSize(re.getSize());

        return Result.success(typeStandardBaseVo);
    }

    @Override
    public Result copy(CopyTypeStandardForm copyTypeStandardForm) {

        String sourceYear = copyTypeStandardForm.getSourceYear();

        String targetYear = copyTypeStandardForm.getTargetYear();
        QueryWrapper<TypeStandard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year",targetYear);
        Long aLong = typeStandardMapper.selectCount(queryWrapper);
        if(aLong>0){
            return Result.fail("复制失败，该年已经存在数据,若想继续复制，请先删除该年的数据");
        }
        else {
            queryWrapper.clear();
            queryWrapper.eq("year",sourceYear);
            List<TypeStandard> typeStandards = typeStandardMapper.selectList(queryWrapper);
            System.out.println("Processed TypeStandards: " + typeStandards); // 添加调试输出
//            typeStandards的每一个元素的year 都赋值sourceYear，使用stream。
            typeStandards = typeStandards.stream()
                    .map(typeStandard -> {
                        typeStandard.setYear(targetYear);
                        typeStandard.setId(null);
                        return typeStandard;
                    })
                    .collect(Collectors.toList());

            System.out.println("Processed TypeStandards: " + typeStandards); // 添加调试输出
            this.saveBatch(typeStandards);
        }
        return Result.success("复制"+sourceYear+"年的年终标准到"+targetYear+"年成功");
    }

    @Override
    public Result addOne(AddTypeStandardForm addTypeStandardForm) {
        TypeStandard typeStandard = new TypeStandard();
        BeanUtil.copyProperties(addTypeStandardForm,typeStandard);
        typeStandardMapper.insert(typeStandard);
        return Result.success("添加成功");
    }

    @Transactional
    @Override
    public Result deleteByIds(List<Integer> ids) {
        typeStandardMapper.deleteBatchIds(ids);
        return Result.success("删除成功");
    }

}




